package com.gis.visualization.config.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.gis.visualization.config.exceptionhandler.VisException;
import com.gis.visualization.entity.EventDesc;
import com.gis.visualization.entity.HistoryEvent;
import com.gis.visualization.entity.HistorySummary;
import com.gis.visualization.entity.vo.EventExcelVo;
import com.gis.visualization.service.*;
import com.gis.visualization.utils.fileutils.FileUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.Map;

public class EventExcelListener extends AnalysisEventListener<EventExcelVo> {
    //    一行一行读取 excel 中的内容 不读表头

    private HistoryEventService eventService;
    private EventDescService descService;
    private HistorySummaryService summaryService;
    private OssService ossService;
    private VideoService videoService;

    public EventExcelListener(HistoryEventService eventService, EventDescService descService, HistorySummaryService summaryService, OssService ossService, VideoService videoService) {
        this.eventService = eventService;
        this.descService = descService;
        this.summaryService = summaryService;
        this.ossService = ossService;
        this.videoService = videoService;
    }

    public EventExcelListener() {
    }

    @Override
    public void invoke(EventExcelVo eventExcelVo, AnalysisContext analysisContext) {
        if (eventExcelVo == null) {
            throw new VisException(20001, "excel文件为空。请检查");
        }
//        事件对象
        HistoryEvent historyEvent = new HistoryEvent();
//        描述对象
        EventDesc eventDesc = new EventDesc();
//        总结对象
        HistorySummary summary = new HistorySummary();

        String imgPath = eventExcelVo.getImgPath();
        String videoPath = eventExcelVo.getVideoPath();

        // 将对象赋值
        BeanUtils.copyProperties(eventExcelVo, historyEvent);

        if (!StringUtils.isEmpty(imgPath)) {
            //   读取图片 上传图片到oss
            String imgUrl = ossService.uploadFileImg(FileUtil.fileToMultipartFile(new File(imgPath)));
            historyEvent.setEventImg(imgUrl);
        }

        if (!StringUtils.isEmpty(videoPath)) {
            //      读取视频
            File videoFile = new File(videoPath);
            String fileName = videoFile.getName();
            String videoId = videoService.uploadVideo(FileUtil.fileToMultipartFile(videoFile));
            summary.setVideoOriginalName(fileName);
            summary.setVideoSourceId(videoId);
        }

//        存储到数据库
        eventService.save(historyEvent);

        //存储到数据库后获取自动生成的id
        String eventId = historyEvent.getId();

        eventDesc.setId(eventId);
        eventDesc.setDescription(eventExcelVo.getDescription());
        eventDesc.setTypes(eventExcelVo.getTypes());
        descService.save(eventDesc);

        summary.setId(eventId);
        summary.setSummary(eventExcelVo.getSummary());
        summaryService.save(summary);

    }

    //    读取完成之后
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    //    读取表头内容
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext analysisContext) {
        System.out.println("表头：" + headMap);
    }
}
